#! /usr/bin/python

import glob, re, string, sys, os, shutil, urllib2

import pepe.p_txt.bigtxt as bt
import pepe.p_filesystem.basic as pfb

def backup_tw_file_tobckp():
    """ """
    FROM_FILE = sorted(pfb.fslist('T:/twiki/', patterns='afttw*.html;', recurse=False))[-2]
    TO_DIR = 'C:/AFT/backup/'

    print FROM_FILE, 'copied to',  TO_DIR
    shutil.copy(FROM_FILE, TO_DIR)

def backup_private_tw_file():
    """ """
    #FROM_DIR = 'C:/AFT/root/TiddlyWiki/'
    FROM_DIR = 'T:/twiki/'
    TO_DIR = 'C:/AFT/backup/'

    # copy/backup files
    FROM_FILE = sorted(pfb.fslist(FROM_DIR, patterns='tw*.html;', recurse=False))[-2]
    #print '\n\n', FROM_FILE, '\n\n'
    print FROM_FILE, 'copied to',  TO_DIR
    shutil.copy(FROM_FILE, TO_DIR)

def backupGDiskAftTw():
    ''' '''
    try:
        FROM_DIR = 'T:/twiki/'
        
        FROM_FILE = sorted(pfb.fslist(FROM_DIR, patterns='afttw*.html;', recurse=False))[-2]
        shutil.copy(FROM_FILE, 'G:/AFT/Forensic-Aft-ISACOPYCHANGESARELOST.html')
        return True
    except:
        return False

def isWebOnline(resource):
    """ check whether web is available """
    #print isOnline(resource='http://www.google.com')
    try:
        response = urllib2.urlopen(resource)
    except:
        return False
    else:
        return True

if __name__ == "__main__":
    """ """
    if os.path.exists(r'G:/AFT/Forensic-Aft-ISACOPYCHANGESARELOST.html') and os.path.exists('T:/twiki/'):
        print 'Copied to G:/AFT/Forensic-Aft-ISACOPYCHANGESARELOST.html : ', backupGDiskAftTw()
    else:
        print 'Is G: and T: attached?'

    if os.path.exists('T:/twiki/'):
        backup_tw_file_tobckp()
    else:
        print 'Is T: attached?'

    os.system('pause')

